from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver

from homework.web.config.base_data import url
from homework.web.model.list_item import ListItem
from homework.web.model.search_condition import SearchCondition
from homework.web.model.user import User
from homework.web.page.base_page import BasePage
from homework.web.utils.log import log


class SearchPage(BasePage):

    def search(self, keyword, type, search_condition: SearchCondition):
        # 输入搜索关键词
        self.send_keys(By.CSS_SELECTOR, 'input[aria-controls="search-result-count"]', keyword)
        # 选择搜索类型
        self.click(By.ID, 'search-type')
        self.click(By.CSS_SELECTOR, f'li[data-value="{type}"]')
        if type == 'topics_posts':
            return self.search_topics_posts(search_condition)
        elif type == 'categories_tags':
            return self.search_categories_tags()
        elif type == 'users':
            return self.search_users()
        else:
            return self.search_topics_posts(search_condition)

    # 搜索主题/帖子
    def search_topics_posts(self, search_condition: SearchCondition):
        # 选择分类
        if search_condition.category is not None:
            self.click(By.CSS_SELECTOR, '#search-in-category')
            self.send_keys(By.CSS_SELECTOR, '#search-in-category-filter > input', search_condition.category)
            self.click(By.CSS_SELECTOR, f'li[title="{search_condition.category}"]')

        # 选择话题
        if search_condition.status is not None:
            self.click(By.CSS_SELECTOR, '#search-status-options')
            self.click(By.CSS_SELECTOR, f'li[title="{search_condition.status}"]')

        # 选择拥有该标签
        if search_condition.tags is not None:
            self.click(By.CSS_SELECTOR, '#search-with-tags')
            for item in search_condition.tags:
                self.send_keys(By.CSS_SELECTOR, '#search-with-tags-filter>input', item)
                self.click(By.CSS_SELECTOR, f'li[title="{item}"]')

        # 选择发帖人
        if search_condition.posted_by is not None:
            self.click(By.CSS_SELECTOR, '#search-posted-by')
            self.send_keys(By.CSS_SELECTOR, '#search-posted-by-filter > input', search_condition.posted_by)
            self.click(By.CSS_SELECTOR, f'li[title="{search_condition.posted_by}"]')

        # 点击搜索按钮
        self.click(By.CSS_SELECTOR, 'button[aria-label="搜索"]')

        # 封装搜索结果
        result_list = []
        elements = self.driver.find_elements(By.CSS_SELECTOR, '[role="listitem"]')
        if len(elements) != 0:
            for item in elements:
                list_item = ListItem()
                # 处理话题
                list_item.topic = item.find_element(By.CLASS_NAME, 'topic-title').text
                # 处理分类
                list_item.category = item.find_element(By.CLASS_NAME, 'category-name').text
                # 处理标签
                tag_list = []
                tag_elements = item.find_elements(By.CSS_SELECTOR, 'a.discourse-tag.box')
                if len(tag_elements) != 0:
                    for element in tag_elements:
                        tag_list.append(element.get_attribute('data-tag-name'))
                list_item.tags = tag_list
                # 处理帖子内容
                list_item.content = item.find_element(By.CSS_SELECTOR, 'div.blurb.container').text

                result_list.append(list_item)
        # log.debug(f'result_list={result_list}')
        return result_list

    # 搜索分类/标签
    def search_categories_tags(self):
        # 点击搜索按钮
        self.click(By.CSS_SELECTOR, 'button[aria-label="搜索"]')
        # 处理搜索结果
        result = {}
        category_list = []
        tag_list = []
        # 处理标签
        tag_elements = self.driver.find_elements(By.CLASS_NAME, 'fps-tag-item')
        if len(tag_elements) != 0:
            for item in tag_elements:
                tag_list.append(item.text)
        result['tag_list'] = tag_list
        # 处理分类
        category_elements = self.driver.find_elements(By.CLASS_NAME, 'category-name')
        if len(category_elements) != 0:
            for item in category_elements:
                category_list.append(item.text)
        result['category_list'] = category_list

        # log.debug(f'result={result}')
        return result

    # 搜索用户
    def search_users(self):
        # 点击搜索按钮
        self.click(By.CSS_SELECTOR, 'button[aria-label="搜索"]')
        # 处理搜索结果
        result_list = []
        elements = self.driver.find_elements(By.CLASS_NAME, 'fps-user-item')
        if len(elements) != 0:
            for item in elements:
                user = User()
                user.avatar = item.find_element(By.CSS_SELECTOR, 'img.avatar').get_attribute('src')
                res = self.is_child_element_exsist(item, By.CLASS_NAME, 'name')
                if res is True:
                    user.name = item.find_element(By.CLASS_NAME, 'name').text
                user.username = item.find_element(By.CLASS_NAME, 'username').text
                result_list.append(user)

        # log.debug(f'result_list={result_list}')
        return result_list

    def back(self):
        # 返回首页
        self.click(By.ID, 'site-logo')
